package cn.newgrand.netcall.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.RolloverButton;
import org.jivesoftware.spark.util.GraphicUtils;

import cn.newgrand.smackx.specialist.ExpertManager;

public class ExpertBaseInfoUI extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private RolloverButton saveButton;
	private RolloverButton cancleButton;
	private JLabel expertnameLabel;
	private JTextField expertnameField;
	private JTextArea welcomeInfoArea;
	private JDialog dialog;
	
	public ExpertBaseInfoUI(final String expertname){
		dialog = new JDialog();
        dialog.setTitle("设置欢迎辞");
        dialog.getContentPane().setLayout(new BorderLayout());
		this.setLayout(new BorderLayout());
		JPanel topPanel = new JPanel(new FlowLayout());
		expertnameLabel = new JLabel("专家号:");
		expertnameField = new JTextField(expertname,12);
		expertnameField.setEditable(false);
		topPanel.add(expertnameLabel);
		topPanel.add(expertnameField);
		JPanel centerPanel = new JPanel(new FlowLayout());
		centerPanel.setBorder(BorderFactory.createTitledBorder("新欢迎辞"));
		//welcomeInfoLabel = new JLabel("欢迎辞:");
		welcomeInfoArea = new JTextArea(8,30);
		//centerPanel.add(welcomeInfoLabel);
		JScrollPane welComepane = new JScrollPane(welcomeInfoArea);
		centerPanel.add(welComepane);
		JPanel bottomPanel = new JPanel();
		saveButton = new RolloverButton("保存");
		saveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String newWelcomeInfo = welcomeInfoArea.getText();
				if(newWelcomeInfo.trim().length() == 0){
					JOptionPane.showMessageDialog(dialog, "欢迎辞不能为空");
				}else{
					ExpertManager.getInstance(SparkManager.getConnection()).setWelcomeInfo(expertname, newWelcomeInfo);
				}
				dialog.dispose();
			}
		});
		cancleButton = new RolloverButton("取消");
		cancleButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
		bottomPanel.add(saveButton);
		bottomPanel.add(cancleButton);
		dialog.getContentPane().add(topPanel,BorderLayout.NORTH);
		this.add(centerPanel,BorderLayout.CENTER);
		final JScrollPane pane = new JScrollPane(this);
	    dialog.getContentPane().add(pane, BorderLayout.CENTER);
	    dialog.getContentPane().add(bottomPanel,BorderLayout.SOUTH);
	    dialog.setLocationRelativeTo(SparkManager.getMainWindow());
	    dialog.pack();
	    dialog.setResizable(true);
        dialog.setSize(400, 280);
        GraphicUtils.centerWindowOnScreen(dialog);
        dialog.setVisible(true);
	}
	
}
